using Domain.Entities;
using Domain.Permissions;
using Domain.Services;
using Domain.Misc;
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using UserInterface.Misc;

namespace UserInterface
{
    public partial class Index : BoardPageBase
    {
        private string currentTemplate;

        protected void Page_Load(object sender, EventArgs e)
        {
            string pageTitle = (string)GetLocalResourceObject("Title");

            Session session = SessionManager.CurrentSession;
            currentTemplate = session.CurrentTemplate;

            MasterPage.TitleGenerator.PageTitle = pageTitle;
            MasterPage.HeaderText = BoardSettingsManager.Instance.Settings.BoardName;
            CategoriesRepeater.DataSource = CategoryService.GetAll();
            CategoriesRepeater.DataBind();
        }

        protected void ForumsRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Forum forum = e.Item.DataItem as Forum;
                ForumPermissionManager forumPermissionManager = PermissionManager.CreateForumPermissionManager(forum);
                if (!forumPermissionManager.Read)
                {
                    e.Item.Visible = false;
                }
                else
                {
                    (e.Item.Parent.Parent as RepeaterItem).Visible = true;
                }
            }
        }

        protected void CategoriesRepeater_ItemCreated(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                e.Item.Visible = false;
            }
        }

        protected IList<Forum> GetForumsByCategory(Category category)
        {
            return ForumService.GetByCategory(category);
        }

        protected string CurrentTemplate
        {
            get { return currentTemplate; }
            set { currentTemplate = value; }
        }
    }
}
